import * as lib from '../../../../../components/aotoo/lib/index';
import {getYmd, lunar, diffDay} from '../../../../../components/modules/calendar/index'

const commonHooks = lib.hooks('calender-announce', true)
const app = getApp()

// 分享弹窗数据存储名
const namespaceOfShareHooks = app.globalData.namespaceOfShareHooks
const preNamespaceOfShareHooks = app.globalData.pre_namespaceOfShareHooks


/**
 * 
 * @returns 显示弹窗，收到的分享内容列表
 */
 export function shareModal(){
  const that = this
  if (this.shareInfo && this.shareInfo.data && this.shareInfo.data.length) {
    const today = getYmd()
    const deletes = []
    const shareInfoModalList = {
      // type: {
      //   is: 'swiper',
      //   "indicator-dots": true
      // },
      // listClass: 'share-swiper',
      // itemClass: 'share-swiper-item'


      type: {
        slip: {
          areaClass: 'share-card',
          areaItemClass: 'share-card-item',
          menuWidth: [140]
        }
      },
      data: [],
      header: {title: '留言列表', itemClass: 'share-scroll-title'},
      listClass: 'share-scroll',
      itemClass: 'share-scroll-item',
      onDeleteItemFromShares(e, param, button){
        const item = button.parent()
        item.remove()
        const sid = param.sid
        const shares = commonHooks.getItem(namespaceOfShareHooks)
        const sharesData = shares.data
        const index = sharesData.findIndex(it=>it.sid === sid)
        if (index > -1) {
          sharesData.splice(index, 1)
          shares.data = sharesData
          commonHooks.setItem(namespaceOfShareHooks, shares)

          const activePage = this.activePage
          const shareButton = activePage.getElementsById('share-info-button')
          shareButton && shareButton.update({
            show: true,
            title: shares.data.length+''
          })
        }
      }
    }

    // 排序
    // this.shareInfo.data = this.shareInfo.data.sort((a, b)=>{
    //   const adate = getYmd(a.date)
    //   const bdate = getYmd(b.date)
    //   return adate.timestamp - bdate.timestamp
    // })

    this.shareInfo.data.forEach(param=>{
      const {alarm, date, hint, content, sid} = param
      const targetDate = getYmd(date)
      const cntBuffer = wx.base64ToArrayBuffer(content)
      const cnt = decodeURIComponent(escape(String.fromCharCode(...(new Uint8Array(cntBuffer)) )));
      const diff = diffDay(today, targetDate)
      if (diff > 0) {
        shareInfoModalList.data.push({
          sid,
          body: [
            {title: hint},
            {
              title: targetDate.timestr+'可查看',
            },
            {
              itemClass: 'iconfont share-swiper-locker',
              aim(){
                wx.showToast({title: targetDate.timestr+'可查看', icon: 'none'})
              }
            }
          ],
        })
      }
      if (diff === 0) {
        shareInfoModalList.data.push({
          sid,
          menus: [
            {
              title: '删除', 
              itemClass: 'delete-share', 
              tap: 'onDeleteItemFromShares?sid='+sid
            }
          ],
          body: [
            {title: hint},
            {
              title: targetDate.timestr + '解锁',
            },
            {title: cnt, itemClass: 'share-content', show: false},
            {
              title: '查看',
              itemClass: 'iconfont share-answer',
              aim(e, param, inst){
                const item = inst.parent()
                const data = item.getData()
                const show = data.body[2].show
                data.body[2].show = !show
                inst.parent().update({
                  body: data.body
                }, function(){
                  inst.update({
                    title: !show ? '关闭' : '查看'
                  })
                })
              }
            },
            // {
            //   title: 'x',
            //   itemClass: 'share-delete'
            // }
          ],
        })
      }
      if (diff < 0) {
        deletes.push(param)
      }

      // swiper方案
      // if (diff > 0) {
      //   shareInfoModalList.data.push({
      //     title: hint,
      //     dot: [{
      //       title: `${targetDate.timestr}可查看`,
      //       itemClass: 'iconfont share-swiper-locker',
      //     }],
      //   })
      // }
      // if (diff === 0) {
      //   shareInfoModalList.data.push({
      //     title: hint,
      //     dot: [cnt],
      //     itemClass: 'share-answer'
      //   })
      // }
      // if (diff < 0) {
      //   deletes.push(param)
      // }

    })

    if (deletes.length) {
      // const shareInfoData = this.shareInfo.data.filter(item=>{
      //   const index = deletes.findIndex(it=>it.date===item.date)
      //   return index === -1
      // })

      // if (shareInfoData.length !== this.shareInfo.data.length) {
      //   this.shareInfo.data = shareInfoData
      //   commonHooks.setItem(namespaceOfShareHooks, this.shareInfo)
      // }
    }

    if (!shareInfoModalList.data.length) return
    const shareButton = this.getElementsById('share-info-button')
    shareButton && shareButton.update({
      show: true,
      title: shareInfoModalList.data.length+''
    })

    wx.modalShare.showModal({
      height: 60,
      width: 85,
      cancelText: that.shareInfo.alarm === false ? '提醒' : '不再提醒',
      itemClass: 'modal-share',
      content: {
        "@list": shareInfoModalList
      },
      success(res){
        if (res.confirm === false) {
          if (that.shareInfo.alarm === false) {
            that.shareInfo.alarm = true
          } else {
            that.shareInfo.alarm = false
          }
          commonHooks.setItem(namespaceOfShareHooks, that.shareInfo)
        }
      }
    })
  }
}

export function openShareModal(){
  if (this.lazyOpenShareModal) return 
  else {
    if (this.shareInfo && this.shareInfo.alarm !== false) {
      this.shareModal()
    }
  }
}